package client;

import gui.Cart_Gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class CustomOrder extends JFrame implements ActionListener
{
	 public JPanel costumPanel = new JPanel();
	 public SpinBox sb = null;
	 public static JComboBox CustomBox = new JComboBox();
	 public JButton cartbutton = new JButton();
	 public JButton Back = new JButton();
	 public JButton Custom = new JButton();
	 public JButton existing = new JButton();
	 public JButton goToCart = new JButton();
	 public JButton logOut = null;
	 public static JComboBox existingCostum = null;
	 JLabel existingCostuml = null;
	 JLabel CostumZerl = null;
	 JLabel CostumTitle = null;
	 JLabel OR = null;
	 public static String Type = null;
	 public String callFrom = null;
	    
	public CustomOrder()
	{
		this.setPreferredSize(new Dimension(400, 460));
		this.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/2 - (int)this.getPreferredSize().getHeight()/2);
		this.setTitle("Build Custom");
		this.setResizable(false);
		costumPanel.setLayout(null);
		
		CostumTitle = new JLabel("Build Custom Item");
		CostumTitle.setBounds(100,60,200,40);
		CostumTitle.setFont(new Font (Font.SANS_SERIF ,Font.BOLD,22));
		costumPanel.add(CostumTitle);
		
		CostumZerl = new JLabel("Create New Custom Item");
		CostumZerl.setBounds(50,110,300,40);
		CostumZerl.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,18));
		costumPanel.add(CostumZerl);
		
		CustomBox = new JComboBox();
		CustomBox.setBounds(50,155,180,40);
		CustomBox.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,18));
		CustomBox.addItem("Zer");
		CustomBox.addItem("Sidur Prahim");
		CustomBox.addItem("Zer Kala");
		costumPanel.add(CustomBox);
		
		Custom = new JButton("Select");
		Custom.setBounds(250,155,100,40);
		Custom.addActionListener(this);
		costumPanel.add(Custom);
			
		OR = new JLabel("OR");
		OR.setBounds(10, 210, 100, 40);
		OR.setFont(new Font (Font.SANS_SERIF ,Font.BOLD,24));
		costumPanel.add(OR);
		
		existingCostuml = new JLabel("Add/Remove From Existing");
		existingCostuml.setBounds(50,250,300,40);
		existingCostuml.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,18));
		costumPanel.add(existingCostuml);
		
		existingCostum = new JComboBox();
		
		if(Main.cart_Control != null)
			Main.cart_Control = null;
		Object[] temp = null;
		Main.cart_Control = new Cart("getData", temp);
		Main.cart_Control = (Cart)Main.clie.getResult();
		
		if(Main.cart_Control.DISTINCTcustomDataNameFromDB.length != 0)
		{
			for(int i = 0 ; i < Main.cart_Control.DISTINCTcustomDataNameFromDB.length ; i++)
				existingCostum.addItem(Main.cart_Control.DISTINCTcustomDataNameFromDB[i]);
		}
		else existingCostum.setEnabled(false);
		
		existingCostum.setBounds(50,295,180,40);
		existingCostum.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,18));
		costumPanel.add(existingCostum);
		
		existing = new JButton("Select");
		existing.setBounds(250,295,100,40);
		existing.addActionListener(this);
		costumPanel.add(existing);
		
		goToCart = new JButton("Go To Cart");
		goToCart.setBounds(50,370,300,40);
		goToCart.setFont(new Font (Font.SANS_SERIF ,Font.PLAIN,18));
		goToCart.addActionListener(this);
		costumPanel.add(goToCart);
		
		logOut = new JButton("LogOut");
		logOut.setBounds(10,10,100,40);
		logOut.addActionListener(this);
		costumPanel.add(logOut);
		
		Back = new JButton("Back");
		Back.setBounds(285,10,100,40);
		Back.addActionListener(this);
		costumPanel.add(Back);
		
		this.add(costumPanel);
		this.pack();
		this.setEnabled(true);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == Back)
		{
			this.setVisible(false);
			Main.catalog.setEnabled(true);
			Main.catalog.setVisible(true);
		}
		
		if(e.getSource() == Custom)
		{
			this.callFrom = "CustomBox";
			int max = 0;
			for(int i = 0 ; i < Main.cart_Control.DISTINCTcustomDataFromDB.length ; i++)
				if(Main.cart_Control.DISTINCTcustomDataFromDB[i] > max) max = Main.cart_Control.DISTINCTcustomDataFromDB[i];
			Main.customId = max+1;
			Type = (String)CustomBox.getSelectedItem();
			sb = new SpinBox(0,0,Integer.MAX_VALUE);
			sb.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/2 - (int)this.getPreferredSize().getHeight()/2);
			this.setEnabled(false);
		}
		if(e.getSource() == existing)
		{
			this.callFrom = "existingCostum";
			int spinBoxVal = 0;
			for(int i = 0 ; i < Main.cart_Control.customSingelItemIdDataFromDB.length ; i++)
			{	
				//itemId = the item id in the selected row
				int itemId = (Integer) Main.catalog.table.getValueAt(Main.catalog.table.getSelectedRow(), 5);
				//set in the spinbox the last value that the user entered  
				if((Integer)Main.cart_Control.customSingelItemIdDataFromDB[i] == itemId 
				&& (Integer)Main.cart_Control.customDataFromDB[i][0] == Integer.parseInt(((String) existingCostum.getSelectedItem()).substring(0, 1)))
				{
					spinBoxVal = (Integer) Main.cart_Control.QuantityCustomDataFromDB[i];
					break;
				}
				else
					spinBoxVal = 0;
			}			
			sb = new SpinBox(spinBoxVal,0,Integer.MAX_VALUE);
			sb.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/2 - (int)this.getPreferredSize().getHeight()/2);
			this.setEnabled(false);
		}
		
		if(e.getSource() == logOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				if(Main.catalog.callFrom == "Search")
				{	
					this.setVisible(false);
					MainScreen.src.setVisible(false);
				}
				Main.catalog.setVisible(false);
				this.setVisible(false);
				Main.main.logOut.doClick();
			}
		}
		
		if(e.getSource() == goToCart)
		{
			if(Main.cart != null)
    			Main.cart.dispose();
			Main.cart = new Cart_Gui("CustomOrder");		//cart = Cart_Gui
			this.dispose();
		}
	}
}
